package com.sercomm.ott.mediaplayer.fragment_test;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.sercomm.ott.mediaplayer.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by davis on 17-10-23.
 */

public class NewsTitleFragment extends Fragment implements AdapterView.OnItemClickListener{
    private ListView newsTitleListView;
    private List<News> newsList;
    private NewsAdapter adapter;
    private boolean isTwoPane;
    @Override
    public void onAttach(Context context){
        super.onAttach(context);
        newsList = getNews();
        adapter = new NewsAdapter(context, R.layout.fragment_news_item,newsList);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_news_title_frag,container,false);
        newsTitleListView = (ListView)view.findViewById(R.id.news_title_list_view);
        newsTitleListView.setAdapter(adapter);
        newsTitleListView.setOnItemClickListener(this);
        return view;
    }
    @Override
    public void onActivityCreated(Bundle saveInstanceState){
        super.onActivityCreated(saveInstanceState);
        if(getActivity().findViewById(R.id.news_content_layout)!=null){
            isTwoPane = true;
        }else {
            isTwoPane = false;
        }
    }
    @Override
    public void onItemClick(AdapterView<?> parent,View view,int position,long id){
        News news = newsList.get(position);
        if(isTwoPane){
            NewsContentFragment newsContentFragment = (NewsContentFragment)getFragmentManager()
                    .findFragmentById(R.id.news_content_fragment);
            newsContentFragment.refresh(news.getTitle(),news.getContent());
        }else {
            NewsContentActivity.actionStart(getActivity(),news.getTitle(),news.getContent());
        }
    }
    private List<News> getNews(){
        List<News> newsList = new ArrayList<News>();
        News news1 = new News();
        news1.setTitle("title1 111111111111");
        news1.setContent("ahfhla kahlfhasf jhjfhjkasdh");
        newsList.add(news1);
        News news2 = new News();
        news2.setTitle("title2 222222222");
        news2.setContent("222222222  222222222222 fasdflk");
        newsList.add(news2);
        return newsList;
    }
}
